/**********************************************************
 * test.c
 *
 *  Copy right (c) 2013, BURCH Co., Ltd.
 *
 *  Created on: 2013-1-1
 *      Author: Elvis.zhang
 *     Version: v0.0.0
 *
 *********************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <poll.h>

#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <pthread.h>

#include "../davinci_spi1.h" 

	FILE 			*file;
typedef struct _tagUserBuf{
	char *buf;
	unsigned int len;
}UsrBufObj, *UsrBufHandler;

int pipeSpi2Net[2];
int pipeNet2Spi[2];

static FILE* orgTsfp = NULL;
void saveOrgTs(char* buf, int len)
{
	if(orgTsfp == NULL)
	{
		orgTsfp = fopen("org.ts", "wb");
	}

	fwrite(buf, 1, len, orgTsfp);
	fflush(orgTsfp);
}

#define FPGA_REST  6

void NetSendThrd(void *arg)
{
	int 				r_size;
	int 				udpfd;
	UsrBufHandler 		rdbuf;
	struct sockaddr_in  addr;
	struct sockaddr_in  cliaddr;
	int i, j;
	unsigned char  key;
	int count = 0;
	
	
	
	while(1)
	{
		r_size = read(pipeSpi2Net[0],&rdbuf,sizeof(rdbuf));
		
#if 1
		for (i = 0; i < 20; i++) 
		{
			if(rdbuf->buf[0 + i * 188] != 0x47)
			{
				printf("0x47 not in header\n");
				printf("count = %d, sum = 0x%x\n", count, count * 188);

			}
			else
			{
				if(rdbuf->buf[5 + i * 188] != rdbuf->buf[184 + i * 188])
				{
					printf("Error data :count = %d  sum = 0x%x key = 0x%x, data = 0x%x\n",
							count, count * 188, rdbuf->buf[5 + i * 188], rdbuf->buf[184 + i * 188]);
					sleep(3);
#if  0
					for(j = 0;j < 188;j++)
					{
									printf("0x%x ", rdbuf->buf[j + (i-1) * 188]);
					}
					putchar('\n');
					for(j = 0;j < 188;j++)
					{
									printf("0x%x ", rdbuf->buf[j + (i) * 188]);
					}
					putchar('\n');
					for(j = 0;j < 188;j++)
					{
									printf("0x%x ", rdbuf->buf[j + (i + 1) * 188]);
					}
					putchar('\n');
#endif
				}
			}
			count++;
		}
#endif
		write(pipeNet2Spi[1],&rdbuf,sizeof(rdbuf));
	}
}

void SpiRevThrd(void *arg)
{
	UsrBufHandler wtbuf;
	unsigned int r_size = 0;
	int				fd;
	SPIBufInfo		bufinfo;
	unsigned int i=0;

	for(i = 0; i < 10; i++)
	{
		wtbuf = malloc(sizeof(UsrBufObj));
		if(wtbuf != NULL)
		{
			wtbuf->buf = malloc(20 * 188);
			if(wtbuf->buf != NULL)
			{
				write(pipeNet2Spi[1],&wtbuf,sizeof(wtbuf));
			}
		}
	}

	while(1)
	{
		read(pipeNet2Spi[0],&wtbuf,sizeof(wtbuf));
		
		r_size = fread(wtbuf->buf,1,188*20,file);
		wtbuf->len = r_size;
		if(r_size <= 0)
		{
						printf("file over\n");
						exit(1);
		}
		write(pipeSpi2Net[1],&wtbuf,sizeof(wtbuf));
	}
}


int main(int argc,char* argv[])
{

	int				i=0,j=0,idx;
	pthread_t 		tid1,tid2;
	
	if( (pipe(pipeSpi2Net) < 0) || (pipe(pipeNet2Spi) < 0) )
		perror("pipe");
	
	pthread_create(&tid1,NULL, (void *)NetSendThrd, NULL);
	pthread_create(&tid2,NULL, (void *)SpiRevThrd, NULL);
	
#if  1
	file = fopen("org.ts","r+");
	if(file == NULL)
	{
		printf("open fail!\n");
	}
#endif
	
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	
	printf("*****exit...\n");
	
	fclose(file);
	close(pipeSpi2Net[0]);
	close(pipeSpi2Net[1]);
	close(pipeNet2Spi[0]);
	close(pipeNet2Spi[1]);
	return 0;
}

